package com.bjbn.service;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.bjbn.model.dto.DevicePicture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Kafka消息发送服务
 */
@Slf4j
@Service
public class KafkaService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${kafka.topics.fids-screen-data}")
    private String fidsScreenDataTopic;

    /**
     * 发送航显屏截图数据到Kafka
     *
     * @param id         设备ID
     * @param picture    Base64编码的图片数据
     * @param updateTime 更新时间
     */
    public void sendFidsScreenData(String id, String picture, Date updateTime) {
        try {
            DevicePicture devicePicture = new DevicePicture();
            devicePicture.setId(id);
            devicePicture.setPicture(picture);
            devicePicture.setUpdateTime(updateTime);

            // 转换为JSON字符串
            String jsonString = JSONObject.toJSONString(devicePicture);

            // 发送到Kafka
            kafkaTemplate.send(fidsScreenDataTopic, id, jsonString);
            log.info("成功发送航显屏截图数据到Kafka，设备ID: {}", id);
        } catch (Exception e) {
            log.error("发送航显屏截图数据到Kafka失败，设备ID: {}", id, e);
        }
    }
}